package edu.ipccenter.toster.ejb;

//import com.sun.xml.ws.api.tx.at.Transactional;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashSet;

import edu.ipccenter.toster.Name;
import edu.ipccenter.toster.Suite;
import edu.ipccenter.toster.TestCase;
import edu.ipccenter.toster.TestUtils;
import edu.ipccenter.toster.jpa.JPATestCase;
import edu.ipccenter.toster.jpa.JPATestSuite;
import edu.ipccenter.toster.jpa.JPATheme;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "TestUtils")
//@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class TestUtilsBean implements TestUtils {

    @PersistenceContext(unitName = "TosterPU")
    private EntityManager em;
    private Collection<TestCase> testCases = new HashSet<TestCase>();
    private Collection<Name> themes = new HashSet<Name>();
    private Collection<Suite> suites = new HashSet<Suite>();

    @Override
    public Collection<Name> getThemes(String keyText, int upperLimit) {

        themes.clear();
        Query q = em.createQuery("select c from JPATheme c where c.tcName LIKE :keyText");
        q.setParameter("keyText", '%' + keyText + '%');

        List<JPATheme> jtest = (List<JPATheme>) q.getResultList();

        for (int i = 0; i < jtest.size() && i < upperLimit; i++) {
            Name n = new Name();
            n.setID(jtest.get(i).getThemeId());
            n.setName(jtest.get(i).getThemeName());
            themes.add(n);
        }


        return themes;
    }

    @Override
    public Collection<Name> getThemes() {
        System.out.println("Em: "+em);
        Query q = em.createQuery("select c from JPATheme c");
        List<JPATheme> tem = q.getResultList();

        themes.clear();
        for (int i = 0; i < tem.size(); i++) {
            Name elem = new Name();

            elem.setID(new BigInteger(tem.get(i).getThemeId() + ""));
            elem.setName(tem.get(i).getThemeName());
            themes.add(elem);
        }


        return themes;
    }


    @Override
    public TestCase getTestCase(BigInteger id) {
        TestCase test=null;
        try {
            List<JPATestCase> testcase;
            Query q = em.createQuery("select c from JPATestCase c where c.tcId =:myid");
            q.setParameter("myid", id);
            testcase = q.getResultList();
            if (testcase.isEmpty()) {
                return null;
            }
            InitialContext ic = new InitialContext();
            test = (TestCase) ic.lookup("java:global/Tost/Tost-ejb/TestCase!edu.ipccenter.toster.TestCase");
            JPATestCase jtest = testcase.get(0);
            test.setTestCase(jtest);


        } catch (NamingException ex) {
            Logger.getLogger(TestUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return test;
    }

    @Override
    public Collection<TestCase> getTestCases() {

        List<JPATestCase> testcase;
        Query q = em.createQuery("select c from JPATestCase c");
        testcase = q.getResultList();
        if (testcase.isEmpty()) {
            return null;
        }
        testCases.clear();
        TestCase test;
        for (int i = 0; i < testcase.size(); i++) {
            try {
                JPATestCase jtest = testcase.get(i);
                InitialContext ic = new InitialContext();
                test = (TestCase) ic.lookup("java:global/Tost/Tost-ejb/TestCase");
                test.setTestCase(jtest);
                testCases.add(test);
            } catch (NamingException ex) {
                Logger.getLogger(TestUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return testCases;
    }

    @Override
    public Collection<Suite> getSuites() {
        suites.clear();

        List<JPATestSuite> testsuit;
        Query q = em.createQuery("select c from JPATestSuite c");
        testsuit = q.getResultList();
        if (testsuit.isEmpty()) {
            return null;
        }

        Suite test;
        for (int i = 0; i < testsuit.size(); i++) {
            try {
                JPATestSuite jtest = testsuit.get(i);
                InitialContext ic = new InitialContext();
                test = (Suite) ic.lookup("java:global/Tost/Tost-ejb/SuiteBean");
                test.setSuite(jtest);
                suites.add(test);
            } catch (NamingException ex) {
                Logger.getLogger(TestUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return suites;
    }

    @Override
    public void addTheme(String Name) {
        JPATheme them = new JPATheme();
        them.setThemeName(Name);
        
        em.merge(them);
                
    }
}
